home *** CD-ROM | disk | FTP | other *** search
/ Aminet 15 / Aminet 15 - Nov 1996.iso / Aminet / dev / basic / ace24dist.lha / ace24.lha / utils / ab2ascii-1.3 / symbols.c < prev    next >
C/C++ Source or Header  |  1996-09-11  |  2KB  |  121 lines

  1. /*
  2.  *  SYMBOLS.C
  3.  */
  4.  
  5. /*
  6.  * (c)Copyright 1994 by Tobias Ferber.
  7.  *
  8.  * This file is part of AmigaBASIC->ASCII.
  9.  *
  10.  * AmigaBASIC->ASCII is free software; you can redistribute it and/or
  11.  * modify it under the terms of the GNU General Public License as
  12.  * published by the Free Software Foundation; either version 1 of the
  13.  * License, or (at your option) any later version.
  14.  *
  15.  * AmigaBASIC->ASCII is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with AmigaBASIC->ASCII; see the file COPYING.  If not, write to
  22.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  */
  24.  
  25. #include "abasic.h"
  26.  
  27. /*
  28.    Allocate a new symbol w/ len+1 bytes for it's '\0' terminated name and link it to sym_tab.
  29.    This function is only called by read_sym()
  30. */
  31.  
  32. static symbol_t *
  33. alloc_sym( len, id )
  34. int len, id;
  35. {
  36.   symbol_t *sym;
  37.  
  38.   if( sym= (symbol_t *)malloc(SYMBOL_SIZE) )
  39.   {
  40.     if( sym->s= (char *)malloc( (len+1) * sizeof(char)) )
  41.     {
  42.       sym->next= sym_tab;
  43.       sym_tab= sym;
  44.  
  45.       sym->id= id;
  46.       sym->s[0]= '\0';
  47.     }
  48.     else
  49.     {
  50.       free(sym);
  51.       sym= NIL(symbol_t *);
  52.     }
  53.   }
  54.  
  55.   return sym;
  56. }
  57.  
  58.  
  59. /*
  60.    Allocate a new symbol via alloc_sym() and read it's name from stdin.
  61.    Return 1 for success, 0 if there was not enough free store.
  62. */
  63.  
  64. int
  65. read_sym( fp, len, id )
  66. FILE *fp;
  67. int len, id;
  68. {
  69.   symbol_t *sym;
  70.  
  71.   if( sym = alloc_sym(len,id) )
  72.   {
  73.     int i;
  74.     char *s= sym->s;
  75.  
  76.     for( i=0; i<len && !feof(fp); i++ )
  77.       s[i]= fgetc(fp);
  78.  
  79.     s[len]= '\0';
  80.   }
  81.  
  82.   return sym ? 1:0;
  83. }
  84.  
  85.  
  86. /*
  87.    Lookup a symbol by it's id in the sym_tab and return a pointer to it's name
  88.    or NIL(char *) if the referenced id does not exist
  89. */
  90.  
  91. char *
  92. get_sym( id )
  93. int id;
  94. {
  95.   symbol_t *sym;
  96.  
  97.   for( sym= sym_tab; sym; sym= sym->next )
  98.     if( sym->id == id )
  99.       return sym->s;
  100.  
  101.   return NIL(char *);
  102. }
  103.  
  104.  
  105. /* free up all memory allocated via alloc_sym() */
  106.  
  107. void
  108. free_symbols( void )
  109. {
  110.   while(sym_tab)
  111.   {
  112.     symbol_t *sym= sym_tab;
  113.     sym_tab= sym_tab->next;
  114.  
  115.     if(sym->s)
  116.       free(sym->s);
  117.  
  118.     free(sym);
  119.   }
  120. }
  121.